package nsn.excel.read;

import nsn.excel.AbstractExcel;
import org.apache.poi.ss.usermodel.Sheet;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 这个抽象类封装别的几种读取的方法
 */
public  class ExcelReadBase extends AbstractExcel implements ExcelRead {

    // 日志打印类
    private static Logger logger = Logger.getLogger(ExcelReadBase.class.getName());


    @Override
    public Map<String,List<Map<String, Object>>> parse(String filePath) {
        try{
            FileInputStream fileInputStream = new FileInputStream(filePath);
            String suffix = filePath.substring(filePath.lastIndexOf(".")+1);
            return parse(fileInputStream,suffix);
        }catch (IOException e){
            logger.log(Level.ALL,"Excel流文件创建失败,请检查目录文件 "+filePath+"是否存在");
            e.printStackTrace();
        }
     return null;
    }

    @Override
    public List<Map<String, Object>> parse(String filePath, Integer sheetNum) {
        try {
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            String suffix = filePath.substring(filePath.lastIndexOf(".")+1);
            Sheet sheet = getSheet(fileInputStream, suffix, sheetNum);
            return getSheetMap(sheet);
        } catch (FileNotFoundException e) {
            logger.log(Level.ALL,"Excel流文件创建失败,请检查目录文件 "+filePath+"是否存在");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map<String,List<Map<String, Object>>> parse(InputStream fileStrem, String suffix) {
         String name="sheet";
        Map<String,List<Map<String, Object>>> resultMap = new HashMap<>();
        List<Sheet> sheetList = getSheetList(fileStrem, suffix);
        for (int i=0;i<sheetList.size();i++) {
            resultMap.put(name+i,getSheetMap(sheetList.get(i)));
        }
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> parse(InputStream fileStrem, String suffix, Integer sheetNum) {
            Sheet sheet = getSheet(fileStrem, suffix, sheetNum);
            return getSheetMap(sheet);
    }


}
